package com.whitedust.top80;

import com.whitedust.structure.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author A
 * @since 2025/11/15 23:29
 */

public class Top72 {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> st = new Stack<>();
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        st.push(root);
        while (!st.isEmpty()) {
            TreeNode pop = st.pop();
            res.add(pop.val);
            if (pop.right != null) st.push(pop.right);
            if (pop.left != null) st.push(pop.left);

        }
        return res;
    }
}
